//
//  ColumnTopView.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/24.
//

import UIKit

protocol ColumnTopViewDelegate{
    
    func selectColumnTopViewDidIndex(index: NSInteger)
}


class ColumnTopView: UIView {
    var delegate: ColumnTopViewDelegate!
    var collectionView: UICollectionView!
    var selectIndex = 1
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        
        let ww = self.frame.size.width/2.5
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: ww, height: 34)
        layout.scrollDirection = .horizontal
    
        self.collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height), collectionViewLayout: layout)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = .white
        self.collectionView.showsVerticalScrollIndicator = false
        self.collectionView.shouldHideToolbarPlaceholder = false
        self.collectionView.register(ColumnTopCell.self, forCellWithReuseIdentifier: "ColumnTopCell")
        self.addSubview(self.collectionView)
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ColumnTopView: UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ColumnTopCell", for: indexPath) as! ColumnTopCell
        switch indexPath.row {
        case 0:
            cell.titleLab.text = MMLocalizedString("tabbar_maintenance", comment: "维修保养")
        default:
            cell.titleLab.text = MMLocalizedString("store", comment: "商城")
        }
        
        if self.selectIndex == indexPath.row{
            cell.titleLab.textColor = Theme.mainColor
            cell.imageView.isHidden = false
            cell.titleLab.font = UIFont(name: Theme.PingFangSCMedium, size: 18)
        }else{
            cell.titleLab.textColor = Theme.titleLabColor
            cell.imageView.isHidden = true
            cell.titleLab.font = UIFont(name: Theme.PingFangSCRegular, size: 14)
        }
        return cell
    }
    

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectIndex = indexPath.row
        self.collectionView.reloadData()
        self.delegate?.selectColumnTopViewDidIndex(index: indexPath.row)
    }
    
}

class ColumnTopCell: UICollectionViewCell {
    var titleLab: UILabel!
    var imageView: UIImageView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        self.clipsToBounds = true
        
        self.titleLab = UILabel(frame: .zero)
        self.titleLab.font = UIFont(name: Theme.PingFangSCMedium, size: 18)
        self.titleLab.textColor = Theme.mainColor
        self.titleLab.textAlignment = .center
        self.contentView.addSubview(self.titleLab)
        
        
        self.imageView = UIImageView(frame: .zero)
        self.imageView.image = UIImage(named: "icon_main_subfield")
        self.contentView.addSubview(self.imageView)
        
        self.titleLab.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(5)
            make.right.equalTo(-5)
            make.height.equalTo(20)
        }
        
        
        self.imageView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLab.snp.bottom)
            make.width.equalTo(21)
            make.centerX.equalTo(self.titleLab.snp.centerX)
            make.height.equalTo(6)
        }
        
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
